package com.none.web.controller.pushTask;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import org.springframework.stereotype.Component;




import com.none.core.common.utils.DateUtil;

import java.util.Timer;


/**
 * 推送定时任务 Bing 创建日期:2015-10-10
 */
@Component
public class TimeTask {
	//还书到期前7天或者过期后1天推送任务
	private TimerTask timeTaskStart = null;
	private Timer timerStart = new Timer();
	//取消的预约记录清空二维码任务
	private TimerTask timeTaskStart_cancel = null;
	private Timer timerStart_cancel = new Timer();


	public void destoryTask() {
		timeTaskStart.cancel();
		timerStart.cancel();
		
		timeTaskStart_cancel.cancel();
		timerStart_cancel.cancel();
	}

	public void start() {
		/***还书到期前7天或者过期后1天推送任务开始***/
		//推送开始时间中午12:00
		int startTime = 12;
		//System.out.println("===========开始定时任务的设置============");
		//当日零点时的毫秒数
		String nowDate = DateUtil.getDate();
		long time = DateUtil.parseDateOnly(DateUtil.getDateChange(nowDate, 0)).getTime();
		
		//下一日零点时的毫秒数
		long time1 = DateUtil.parseDateOnly(DateUtil.getDateChange(nowDate, 1)).getTime();

		//现在的毫秒数
		long time2 = System.currentTimeMillis();
		
		//推送的延迟时间
		long time3 = 0l;
		
		//计算推送的延迟时间
		if((time2-time)<1000*60*60*startTime){//如果当天推送时间未到的情况,则计算到当日推送的时间
			time3 = 1000*60*60*startTime+time-time2;
		}else{//如果当天推送时间已过,则计算到下一日推送的时间.
			time3 =  time1 - time2 + 1000 * 60 * 60 * startTime;
		}
		
		//推送定时任务设置
		try {
			timeTaskStart = PushStartTask.getInstance();
			timerStart.schedule(timeTaskStart, time3, 1000 * 60 * 60 * 24); // 任务、时延、间隔时间

			System.out.println("===========推送定时任务还需要"+time3/1000/60/60+"个小时"+(time3/1000/60)%60+"分"+(time3/1000)%3600%60+"秒开始执行推送============");
		} catch (Exception e) {
			e.printStackTrace();
		}
		/***还书到期前7天或者过期后1天推送任务结束***/
		
		/***取消的预约记录清空二维码任务开始***/
		//执行时间 0点
		int startTime_cancel = 0;
		//System.out.println("===========开始定时任务的设置============");
		//当日零点时的毫秒数
		String nowDate_cancel = DateUtil.getDate();
		long time_cancel = DateUtil.parseDateOnly(DateUtil.getDateChange(nowDate_cancel, 0)).getTime();
		
		//下一日零点时的毫秒数
		long time1_cancel = DateUtil.parseDateOnly(DateUtil.getDateChange(nowDate_cancel, 1)).getTime();
		
		//执行的延迟时间
		long time3_cancel = 0l;
		//现在的毫秒数
		long time2_cancel = System.currentTimeMillis();
		//计算执行的延迟时间
		if((time2_cancel-time_cancel)<1000*60*60*startTime_cancel){//如果当天执行时间未到的情况,则计算到当日执行的时间
			time3_cancel = 1000*60*60*startTime_cancel+time_cancel-time2_cancel;
		}else{//如果当天执行时间已过,则计算到下一日执行的时间.
			time3_cancel =  time1_cancel - time2_cancel + 1000 * 60 * 60 * startTime_cancel;
		}
		
		//清空二维码任务设置
		try {
			timeTaskStart_cancel = DelQRcodeTask.getInstance();
			timerStart_cancel.schedule(timeTaskStart_cancel, time3_cancel, 1000 * 60 * 60 * 24); // 任务、时延、间隔时间

			System.out.println("===========清空任务还需要"+time3_cancel/1000/60/60+"个小时"+(time3_cancel/1000/60)%60+"分"+(time3_cancel/1000)%3600%60+"秒开始执行清空任务");
		} catch (Exception e) {
			e.printStackTrace();
		}
		/***取消的预约记录清空二维码任务结束***/
	}

	public static void main(String args[]) {
		//new TimeTask().start();
		String rec_date = "20140610";
		
		List<String> accList = new ArrayList<String>();
		accList.add("main_acc_no1|20140610");
		accList.add("main_acc_no2|20140610");
		accList.add("main_acc_no3|20140610");
		
		File accFile = new File("d:/main_count_"+rec_date+".txt");
		FileWriter fi = null;
		try{
			fi = new FileWriter(accFile);
			for(int i=0; i<accList.size();i++){
				fi.write(accList.get(i));
				fi.write("\n");
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				fi.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
